package top.superflow.flow.runtime.flow

import org.apache.commons.lang3.builder.ToStringBuilder
import top.superflow.exception.Exceptable
import top.superflow.flow.runtime.inf.GeneralResponse
import top.superflow.lang.{CollectionUtils, StringUtils}

import scala.collection.mutable

class FlowResponse extends GeneralResponse {


  private val allResponses = mutable.LinkedHashMap[String, GeneralResponse]()

  def getLastResponse : GeneralResponse = {
    if(CollectionUtils.isEmpty(allResponses)){
      return null
    }

    allResponses.last._2
  }

  def getAllResponses : Map[String, GeneralResponse] = allResponses.toMap

  def getResponse(routeKey : String) : Option[GeneralResponse] = {
    allResponses.get(routeKey)
  }

  def getRoutePath : Iterable[String] = allResponses.keys

  def addResponse(routeKey : String, gr : GeneralResponse) = {
    if(gr == null || StringUtils.isBlank(routeKey)){
      throwRequestUnsupportedException("The route can't be blank or the general response can't be null")
    }

    if(allResponses.contains(routeKey)){
      throwRequestUnsupportedException("The is already a response for the route key {}, please rename the route in the route configuration", routeKey)
    }

    this.allResponses += (routeKey -> gr)
  }

  private def addResponses(responses : Map[String, GeneralResponse]) :Unit = {
    if(CollectionUtils.isEmpty(responses)){
      return
    }
    this.allResponses ++= responses
  }

  override def clone(): FlowResponse = {
     val flowResponse = new FlowResponse

    flowResponse.addResponses(this.allResponses.toMap)

    flowResponse

  }
}
